package com.lh.music.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lh.music.R;
import com.lh.music.entity.Song;

import java.util.List;

/**
 * Created by a on 2016/12/13.
 */

public class RecyclAdapter extends RecyclerView.Adapter<RecyclAdapter.MyHolder> {
    private Context context;
    private List<Song> mData;
    private OnItemClickListener onItemClickListener;
    private OnMoreClickListener onMoreClickListener;
    public RecyclAdapter(Context context, List<Song> mData) {
        this.context = context;
        this.mData = mData;
    }

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public OnMoreClickListener getOnMoreClickListener() {
        return onMoreClickListener;
    }

    public void setOnMoreClickListener(OnMoreClickListener onMoreClickListener) {
        this.onMoreClickListener = onMoreClickListener;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=LayoutInflater.from(context)
                .inflate(R.layout.adapter_danqu,null);
        final MyHolder holder=new MyHolder(view);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (onItemClickListener!=null){
                    onItemClickListener.onItemClickListener((Song) v.getTag(),holder.getLayoutPosition());
                }
            }
        });

        return holder;
    }

    @Override
    public void onBindViewHolder(MyHolder holder, final int position) {
        //控制小喇叭的显示隐藏
        if (mData.get(position).isVisiable()){
            holder.play.setVisibility(View.VISIBLE);
        }else{
            holder.play.setVisibility(View.GONE);
        }
        holder.name.setText(mData.get(position).getTitle());
        holder.artist.setText(mData.get(position).getArtist()+"-"+mData.get(position).getAlbum());
        holder.itemView.setTag(mData.get(position));
        holder.more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onMoreClickListener!=null){
                    onMoreClickListener.onMoreClickListtener(mData.get(position));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    static class MyHolder extends RecyclerView.ViewHolder{
        ImageView play;
        TextView name;
        TextView artist;
        ImageView more;
        public MyHolder(View itemView) {
            super(itemView);
            play= (ImageView) itemView.findViewById(R.id.play);
            more= (ImageView) itemView.findViewById(R.id.more);
            name= (TextView) itemView.findViewById(R.id.name);
            artist= (TextView) itemView.findViewById(R.id.artist);
        }
    }

    /**
     * 对外提供每一项的点击事件
     */
    public interface OnItemClickListener{
        void onItemClickListener(Song song,int position);
    }
    /**
     * 对外提供more点击事件的接口
     */
    public interface  OnMoreClickListener{
        void onMoreClickListtener(Song song);
    }
}
